﻿Imports System.Windows.Forms

Public Class CCMDialog

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FolderBrowser.Description = "Please select a folder where you would like to save the CCM output files"
        If FolderBrowser.ShowDialog() = Windows.Forms.DialogResult.OK Then
            FolderBox.Text = FolderBrowser.SelectedPath & "\"

        End If
    End Sub

    Private Sub CancelBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelBut.Click
        Me.Close()
    End Sub

    Private Sub NextBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NextBut.Click

        If TheTabs.SelectedTab.Name = "FilterTab" Then
            If ProviderFilter.zFilterCount <> 0 Then

                ProviderFilter.ClearFilter()

                Dim count = ProviderFilter.zRecordCount

                ProviderFilter.ApplyFilter()


                If ProviderFilter.zRecordCount = count Then
                    MessageBox.Show("No providers have been filtered by the current filter.  If you want to select all providers, then remove all filters.", "Nothing filtered", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Exit Sub
                End If

                If ProviderFilter.zRecordCount = 0 Then
                    MessageBox.Show("No providers will be produced by this filter.  Please change your filters.", "No providers selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Exit Sub
                End If

            End If
        End If
       

        If TheTabs.SelectedIndex < TheTabs.TabCount - 1 Then
            TheTabs.SelectedIndex = TheTabs.SelectedIndex + 1
            Backbut.Enabled = True
        End If

        If TheTabs.SelectedIndex = TheTabs.TabCount - 1 Then
            NextBut.Enabled = False
        Else
            NextBut.Enabled = True
        End If

      
    End Sub

    Private Sub Backbut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Backbut.Click
        If TheTabs.SelectedIndex <> 0 Then
            TheTabs.SelectedIndex = TheTabs.SelectedIndex - 1
            NextBut.Enabled = True
        End If


        If TheTabs.SelectedIndex = 0 Then
            Backbut.Enabled = False
        Else
            Backbut.Enabled = True
        End If
    End Sub

    Private Sub CCMDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Hide the tabs
        TheTabs.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed
        TheTabs.ItemSize = New System.Drawing.Size(0, 1)
        TheTabs.SizeMode = System.Windows.Forms.TabSizeMode.Fixed

        'Load the boxes

        Dim sql As String

        'Region Box
        sql = "Select 'All' as Region_Code, '<All>' as Region_name union all select Region_Code, Region_Name from Regions"

        RegionBox.DataSource = LoadDataView(sql)
        RegionBox.ValueMember = "Region_Code"
        RegionBox.DisplayMember = "Region_Name"

        'Type Box
        sql = "Select 'All' as Provider_Type_Code, '<All>' as Provider_Type_Description union all select Provider_Type_Code, Provider_Type_Description from provider_types where provider_type_code <> 'Blank'"

        ProviderTypeBox.DataSource = LoadDataView(sql)
        ProviderTypeBox.ValueMember = "Provider_Type_Code"
        ProviderTypeBox.DisplayMember = "Provider_Type_Description"

        'period box
        sql = "Select * from Periods order by Period_ID desc"

        ThePeriodBox.DataSource = LoadDataView(sql)
        ThePeriodBox.ValueMember = "Period_ID"
        ThePeriodBox.DisplayMember = "Period_Name"

        ThePeriodBox.SelectedValue = CurrentPeriodID

        LoadProviderBox("", "")

        LoadPaymentdatesbox()

        LoadFundingStreamBox()


        'filter datasource
        Dim DS As DataView

        Dim p As New List(Of Object)
        p.Clear()

        p.Add("Select * from View_Provider_Filter_Options")
        p.Add("Preparing the provider filter data, please wait...")

        DoSomeWork("LoadBusyDataView", p, False)

        DS = BusyButFine.ReturnedValues(0)

        '  DS = LoadDataView("Select * from View_Provider_Filter_Options")
        ProviderFilter.zDataView = DS
        ProviderFilter.ClearFilter()

    End Sub

    Private Sub LoadFundingStreamBox()
        Dim sql As String

        sql = "select '' as CCM_Funding_Stream, 'All Funding Streams' as Description union select CCM_Funding_Stream, Description from CCM_Funding_Streams"

        LoadComboBox(FundingStreamBox, sql, "CCM_Funding_Stream", "Description")


    End Sub

    Private Sub LoadDeliverableBox()

        Dim sql As String

        Try

            If FundingStreamBox.SelectedValue = "" Then
                sql = "select 0 as DeliverableCode, 'All Deliverables' as Deliverables"

            Else
                sql = "select 0 as DeliverableCode, 'All Deliverables' as Deliverables union select DeliverableCode, Deliverables from CCM_Deliverables_MultiYear where CCM_Funding_Stream = '" & FundingStreamBox.SelectedValue & "' and Period_ID = " & ThePeriodBox.SelectedValue & " order by DeliverableCode"
            End If


        Catch ex As Exception
            Exit Sub
        End Try

        LoadComboBox(DeliverableBox, sql, "DeliverableCode", "Deliverables")


    End Sub

    Private Sub StartBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartBut.Click
        'Validate
        'check the folder
        If Trim(FolderBox.Text) = "" Then
            MessageBox.Show("The folder you selected does not exist or is not accessible, please select another folder", "The folder does not exist", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Dim dir As New IO.DirectoryInfo(FolderBox.Text)

        If Not dir.Exists Then
            MessageBox.Show("The folder you selected does not exist or is not accessible, please select another folder", "The folder does not exist", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        ButtonPanel.Enabled = False
        StartBut.Enabled = False
        Me.Cursor = Cursors.WaitCursor

        Me.Refresh()

      
        Dim RID As String = ""

        If ProviderBox.SelectedValue <> "0" Then
            RID = ProviderBox.SelectedValue.ToString

        End If

        If CCMRadio.Checked Then

            If ThePeriodBox.SelectedValue >= 2012131 Then
                'CCMFile_New(ThePeriodBox.SelectedValue, FolderBox.Text, RegionBox.SelectedValue.ToString, ProviderTypeBox.SelectedValue, 0)

                Dim UPIN As String = ""

                If ProviderFilter.zFilterCount <> 0 Then
                    'a filter has been applied
                    ProviderFilter.ClearFilter()

                    Dim count As Integer

                    count = ProviderFilter.zRecordCount

                    ProviderFilter.ApplyFilter()

                    If ProviderFilter.zRecordCount <> 0 Then
                        If count <> ProviderFilter.zRecordCount Then

                            Dim str(ProviderFilter.zRecordCount - 1) As String

                            For i = 0 To ProviderFilter.zRecordCount - 1

                                str(i) = ProviderFilter.zDataView.Item(i)("UPIN").ToString

                            Next


                            UPIN = String.Join(",", str)
                        End If
                    Else
                        UPIN = ""
                    End If


                End If

                Dim Params As New List(Of Object)

                Params.Clear()
                Params.Add(ThePeriodBox.SelectedValue)
                Params.Add("") 'provider type.  This needs to be determined by the providers selected
                Params.Add("") 'territory code
                Params.Add(IIf(RegionBox.SelectedValue.ToString = "All", "", RegionBox.SelectedValue.ToString))
                Params.Add("") 'LA Code
                Params.Add(UPIN) 'UPIN
                Params.Add("") 'Budget_ID list
                Params.Add(DeliverableBox.SelectedValue) ' DeliverableCode
                Params.Add(FundingStreamBox.SelectedValue) ' FundingStream
                Params.Add(FolderBox.Text) 'Path

                If DoSomeWork("CCMFile", Params) = Windows.Forms.DialogResult.OK Then
                    MessageBox.Show("The CCM Payment files have been produced.", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If

            Else
                CCMFile(ThePeriodBox.SelectedValue, FolderBox.Text, RegionBox.SelectedValue.ToString, ProviderTypeBox.SelectedValue.ToString, RID)
            End If


        Else
            Dim TheMonth As Integer = 0
            Dim TheYear As Integer = 0

            TheMonth = Mid(PaymentDateBox.SelectedValue, 1, PaymentDateBox.SelectedValue.ToString.IndexOf("-"))
            TheYear = Mid(PaymentDateBox.SelectedValue, PaymentDateBox.SelectedValue.ToString.IndexOf("-") + 2, 4)

          


            CODAFile(ThePeriodBox.SelectedValue, FolderBox.Text, RegionBox.SelectedValue.ToString, ProviderTypeBox.SelectedValue.ToString, TheMonth, TheYear, RID)
        End If

        StartBut.Enabled = True
        ButtonPanel.Enabled = True
        Me.Cursor = Cursors.Default

        Me.Close()

    End Sub

    Private Sub CodaRadio_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CodaRadio.CheckedChanged
        If CCMRadio.Checked Then
            PaymentDateBox.Enabled = False
        Else
            PaymentDateBox.Enabled = True
        End If
    End Sub

    Private Sub CCMRadio_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CCMRadio.CheckedChanged
        If CCMRadio.Checked Then
            PaymentDateBox.Enabled = False
        Else
            PaymentDateBox.Enabled = True
        End If
    End Sub

    Private Sub ThePeriodBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThePeriodBox.SelectedIndexChanged
        LoadPaymentdatesbox()

        Try
            If ThePeriodBox.SelectedValue >= 2012131 Then
                HideMePanel.Visible = False
                FundingStreamPanel.Visible = True
                LoadDeliverableBox()
            Else
                HideMePanel.Visible = True
                FundingStreamPanel.Visible = False
            End If

            If ThePeriodBox.SelectedValue <= 2011121 Then
                Pre12Panel.Visible = True
                Post12Panel.Visible = False

            Else
                Pre12Panel.Visible = False
                Post12Panel.Visible = True

            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub LoadPaymentdatesbox()
        Dim sql As String

        Dim TheView As DataView

        Try
            sql = "select distinct cast(month(payment_date) as nvarchar(10)) + '-' + cast(year(payment_Date) as nvarchar(10)) as PaymentPeriod, year(payment_Date) as TheYear, month(payment_Date) as TheMonth from payment_dates where payment_id in (select Payment_ID from payment_types where period_id = " & ThePeriodBox.SelectedValue & ") order by TheYear, TheMonth"
        Catch ex As Exception
            Exit Sub
        End Try

        TheView = LoadDataView(sql)

        PaymentDateBox.DataSource = TheView
        PaymentDateBox.ValueMember = "PaymentPeriod"
        PaymentDateBox.DisplayMember = "PaymentPeriod"
    End Sub

  
    Private Sub ProviderBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProviderBox.SelectedIndexChanged
        If ProviderBox.SelectedValue.ToString = "1" Then
            Search_Form.WhoSentForMe = "CCMDialog"
            If Search_Form.ShowDialog = Windows.Forms.DialogResult.OK Then
                With Search_Form
                    LoadProviderBox(.SearchResults_DataViewGrid.Item("RID", .SearchResults_DataViewGrid.CurrentCell.RowIndex).Value, .SearchResults_DataViewGrid.Item("Provider", .SearchResults_DataViewGrid.CurrentCell.RowIndex).Value)
                    ProviderBox.SelectedValue = .SearchResults_DataViewGrid.Item("RID", .SearchResults_DataViewGrid.CurrentCell.RowIndex).Value
                    RegionBox.SelectedValue = .SearchResults_DataViewGrid.Item("Region_Code", .SearchResults_DataViewGrid.CurrentCell.RowIndex).Value
                End With
            End If
        End If
    End Sub

    Private Sub LoadProviderBox(ByVal RID As String, ByVal Provider_Name As String)

        Dim sql As String = ""

        sql = "select '0' as RID, '<All>' as Provider_Name "
        sql = sql & " UNION ALL select '1' as RID, 'Select a provider...' as Provider_Name "

        If RID <> "" Then

            If Provider_Name.Contains("'") Then
                Provider_Name = Provider_Name.Replace("'", "''")
            End If

            sql = sql & " UNION ALL select '" & RID & "' as RID, '" & Provider_Name & "' as Provider_Name "
        End If

        LoadComboBox(ProviderBox, sql, "RID", "Provider_Name")

    End Sub

    Private Sub CCMDialog_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        If ThePeriodBox.SelectedValue >= 2012131 Then
            HideMePanel.Visible = False
        Else
            HideMePanel.Visible = True
        End If

    End Sub

  
   
    Private Sub Pre12Panel_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Pre12Panel.Paint

    End Sub

   
   
    Private Sub FundingStreamBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FundingStreamBox.SelectedIndexChanged
        LoadDeliverableBox()

    End Sub
End Class
